<%-- 
    Document   : search
    Created on : May 7, 2013, 11:00:28 PM
    Author     : phdinusa
--%>

<%@page import="java.util.Vector"%>
<%@page import="java.util.Hashtable"%>
<%@page import="db.ActorObj"%>
<%@page import="db.MovieObj"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="db.DB"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>DataFlix - Search</title>

            <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
            <meta http-equiv="content-language" content="en" />
            <meta name="description" content="" />
            <meta name="language" content="en" />

            <link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
            <link type="text/css" rel="stylesheet" href="./styles/header.css"/>
            <link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
            <link type="text/css" rel="stylesheet" href="./styles/images.css"/>

            <link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
            <link type="text/css" rel="stylesheet" href="./styles/category.css"/>


    </head>
    <body>

        <jsp:include page="./header.jsp" />
        
        <div id="titleBest">Results for “<%= request.getParameter("searchQur") %>”</div>

        
        <ul class="contentRow" id="Best">
        <%
            // split the input by space
            String searchQur = request.getParameter("searchQur");
            String[] searchkeywords = searchQur.split(" ");
            
            String path=null;   
            File f=null;    
            FileInputStream fis=null;   
            InputStreamReader inStream=null;    
            BufferedReader buff=null;
            
            DB data=new DB();
            Connection conn=null;
            conn = data.getConnection();
            Statement stm = conn.createStatement();
           
            int num = 1;// number of search results;
            int mol = 0;
            
            String previous = "";// the previous search result;
            
            Hashtable mHash=new Hashtable();
            Hashtable aHash=new Hashtable();
            Vector mKeys=new Vector();
            Vector aKeys=new Vector();
            for(int i=0;i<searchkeywords.length;i++)
            {
                String currentKeyword = searchkeywords[i];
                String quer = "SELECT Name, Id, Type FROM Movie WHERE Name LIKE '%"+currentKeyword+"%'";
                ResultSet rs = stm.executeQuery(quer);
                MovieObj mTmp=null;
                while(rs.next())
                {    
                    // search all the movie titles
                    String title = rs.getString(1);
                    if(!title.equals(previous))
                    {
                        int mId = rs.getInt(2);
                        String genre = rs.getString(3);                        
                        
                        mTmp=new MovieObj(mId, title, genre);
                        mHash.put(mId, mTmp);
                        if (!mKeys.contains(mId))
                            mKeys.add(mId);
                        
                        num++;
                        previous = title;
                    }
                }
                
                quer = "SELECT Name, Id FROM Actor WHERE Name LIKE '%"+currentKeyword+"%'";
                rs = stm.executeQuery(quer);
                ActorObj aTmp=null;
                while(rs.next())
                {
                    // search all the actor names
                    String actorName = rs.getString(1);
                    if(!actorName.equals(previous))
                    {
                        int aId = rs.getInt("Id");
                        
                        aTmp=new ActorObj(aId, actorName, 'm', 97);
                        aHash.put(aId, aTmp);
                        if (!aKeys.contains(aId))
                            aKeys.add(aId);
                        
                        num++;
                        previous = actorName;
                    }
                }
            }
            int j=0;
            for (int s=0; s<mHash.size(); s++)
            {
                MovieObj mobj=(MovieObj)mHash.get(mKeys.get(s));
                %>
                <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./movie.jsp?id="+mobj.getId()+"\""%>>
                        <img id=<%="movie"+mobj.getId()%> <%="style=\"background-image: url(./images/movies/"+mobj.getGenre().toLowerCase()+"/movie"+mobj.getId()+".jpg\""%>/>
                        <div class="overlay">
                            <h1><%=mobj.getTitle()%></h1>
                            <div class="shortDesc">
                                <%
                                    path=application.getRealPath("/");
                                    f=new File(path+"images/movies/"+mobj.getGenre().toLowerCase()+"/movie"+mobj.getId()+"Desc.txt");
                                    fis=new FileInputStream(f);
                                    inStream = new InputStreamReader(fis);
                                    buff = new BufferedReader(inStream);

                                    path=buff.readLine();
                                    out.print(path.substring(0, 205)+"...");
                                %>
                            </div>
                        </div>
                    </a>
                </li>       
            <%
                j++;
                 
                if (j==6)
                    j=0;
            }
            j=0;
            for (int s=0; s<aHash.size(); s++)
            {
                ActorObj aobj=(ActorObj)aHash.get(aKeys.get(s));
             %>
                <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./actor.jsp?id="+aobj.getId()+"\""%>>
                        <img id=<%="actor"+aobj.getId()%> <%="style=\"background-image: url(./images/actors/actor"+aobj.getId()+".jpg\""%>/>
                        <div class="overlay">
                            <h1><%=aobj.getName()%></h1>
                            <div class="shortDesc">
                                <%
                                    path=application.getRealPath("/");
                                    f=new File(path+"images/actors/actor"+aobj.getId()+".txt");
                                    fis=new FileInputStream(f);
                                    inStream = new InputStreamReader(fis);
                                    buff = new BufferedReader(inStream);

                                    path=buff.readLine();
                                    out.print(path.substring(0, 205)+"...");
                                %>
                            </div>
                        </div>
                    </a>
                </li>
            <%                                 
            }
            %>
            
            
        </ul>
        
        <%--<div id="pageBtns">
            <a class="bestBtn" href="./">&#8617; Previous page</a>
            <a class="bestBtn" href="./">Next page &#10143;</a>
        </div>--%>

        <div id="footer"></div>
    </body>
</html>
